﻿CREATE PROCEDURE [dbo].[smenu__Action] 
	@ID sysname,
	@FromID int,
	@ToID int
AS
SET NOCOUNT ON

IF @FromID > 0 AND dbo.fs_Workflow_Check(@FromID,@ToID) = 0 BEGIN DECLARE @SF sysname,@ST sysname	SELECT @SF = MenuName FROM t_Menu WHERE MenuID = @FromID SELECT @ST = MenuName FROM t_Menu WHERE MenuID = @ToID	RAISERROR('There is no path from [%s] to [%s].',16,1,@SF,@ST)	ROLLBACK TRAN RETURN END

DECLARE @Action nvarchar(4000),@Res int
DECLARE cur CURSOR LOCAL FOR
SELECT Action FROM t_MenuEvent
WHERE(MenuFromID = @FromID AND MenuToID = @ToID  OR
			MenuFromID IS NULL AND MenuToID = @ToID OR
			MenuFromID = @FromID AND MenuToID IS NULL
		 ) AND Pos > 0
ORDER BY Pos

OPEN cur

FETCH NEXT FROM cur INTO @Action

WHILE @@FETCH_STATUS = 0
BEGIN
	--PRINT dbo.sprint4('@ID:%s,@StatusFromID:%s,@StatusToID:%s,Action:%s',@ID,@FromID,@ToID,@Action)
	SET @Res = 0
	EXEC @Res = sp_executesql @Action,N'@ID sysname',@ID
	IF @@ERROR <> 0  OR @Res <> 0 BEGIN
		IF @@TRANCOUNT > 0 ROLLBACK TRAN
		BREAK
	END
	FETCH NEXT FROM cur INTO @Action
END

CLOSE cur
DEALLOCATE cur






